package com.wt.springsamples.rabbitmq.demo;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class MessageProducerTest {
    private static  final String EXCHANGE_NAME="exchange_demo";
    private static  final String ROUTING_KEY="routingKey_demo";
    private static  final String QUEUE_NAME="queue_demo";
    private static  final String IP_ADDRESS="127.0.0.1";
    private static  final int PORT=5672;
    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost(IP_ADDRESS);
        factory.setPort(PORT);
        factory.setUsername("guest");
        factory.setPassword("guest");
        factory.setVirtualHost("/");
        Connection connection = factory.newConnection();
        //channel不能在线程之间共享
        Channel channel = connection.createChannel();
        channel.exchangeDeclare(EXCHANGE_NAME,"direct");
        channel.queueDeclare(QUEUE_NAME,true,false,false,null);
        channel.queueBind(QUEUE_NAME,EXCHANGE_NAME,ROUTING_KEY);//此处的BINDING_KEY 等于 ROUTING_KEY

        int i=0;
        while (true){
            i++;
            String message = "hello world "+String.valueOf(i);
            try {
                TimeUnit.SECONDS.sleep(1);
            }catch (InterruptedException e){
                e.printStackTrace();
            }
            channel.basicPublish(EXCHANGE_NAME,ROUTING_KEY, MessageProperties.PERSISTENT_TEXT_PLAIN,message.getBytes());
//            channel.basicPublish(EXCHANGE_NAME,ROUTING_KEY,
//                    new AMQP.BasicProperties.Builder()
//                            .contentType("text/plain")
//                            .userId("")
//                            .build()
//                    ,message.getBytes());
            if(1>3){
                break;
            }
        }
        channel.close();
        connection.close();
    }
}
